#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<int> findOrder(int n, vector<vector<int>>& prerequisites) {
        vector<int> ret;
        if (n == 1) { ret.push_back(0); return ret; }
        unordered_map<int, vector<int>> edges;
        vector<int> in(n);
        for (int i = 0; i < prerequisites.size(); i++)
            edges[prerequisites[i][1]].push_back(prerequisites[i][0]), in[prerequisites[i][0]]++;
        queue<int> q;
        for (int i = 0; i < in.size(); i++) if (in[i] == 0) q.push(i), in[i] = -1;
        while (q.size())
        {
            int tmp = q.front();
            q.pop();
            for (auto i : edges[tmp])
                if (--in[i] == 0) q.push(i);
            ret.push_back(tmp);
        }
        return ret.size() == n ? ret : vector<int>();
    }
};